define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'manufacture/equipment/index' + location.search,
                    add_url: 'manufacture/equipment/add',
                    edit_url: 'manufacture/equipment/edit',
                    del_url: 'manufacture/equipment/del',
                    multi_url: 'manufacture/equipment/multi',
                    table: 'equipment',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'equipment_code', title: __('设备编码'), operate: 'LIKE'},
                        {field: 'equipment_name', title: __('设备名称'), operate: 'LIKE'},
                        {field: 'category.category_name', title: __('分类'), operate: false},
                        {field: 'model', title: __('型号'), operate: 'LIKE'},
                        {field: 'status', title: __('状态'), searchList: {"1":__('正常'),"2":__('维修中'),"3":__('故障'),"4":__('报废')}, formatter: function(value, row, index) {
                            var statusMap = {1: '正常', 2: '维修中', 3: '故障', 4: '报废'};
                            var classMap = {1: 'success', 2: 'warning', 3: 'danger', 4: 'default'};
                            return '<span class="label label-' + classMap[value] + '">' + statusMap[value] + '</span>';
                        }},
                        {field: 'location', title: __('位置'), operate: 'LIKE'},
                        {field: 'createtime', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate, formatter: function(value, row, index) {
                            var table = this.table;
                            var buttons = [];
                            buttons.push({
                                name: 'maintenance',
                                text: __('维护'),
                                title: __('设备维护'),
                                classname: 'btn btn-xs btn-warning btn-dialog',
                                icon: 'fa fa-wrench',
                                url: 'manufacture/equipment/maintenance/ids/{ids}',
                                callback: function (data) {
                                    table.bootstrapTable('refresh');
                                }
                            });
                            return Table.api.formatter.operate.call(this, value, row, index, buttons);
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        maintenance: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
